//
//  ss.m
//  360Safe
//
//  Created by zhikuiyu on 14/11/3.
//  Copyright (c) 2014年 baidu. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString(Regex)

- (NSString *)transformToPinyin {
    NSMutableString *mutableString = [NSMutableString stringWithString:self];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    mutableString = (NSMutableString *)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    return mutableString;
}

- (RegexState)regexPhoneNumber{
    NSString * regex = @"^[1][3758][0-9]{9}$";
    
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];

    BOOL isMatch = [pred evaluateWithObject:self];
    if(isMatch){

        return RegexStateRight;
    }
    
    int length = (int)self.length ;
    if(length < 11){
        return RegexStateLengthToLessError;
    }else if(length > 11){
        return RegexStateLengthToMoreError;
    }

    
    return RegexStateChatError;
}

- (BOOL)regexidenCodeNumber{
    NSString * regex = @"^[0-9]{4}$";
    
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    BOOL isMatch = [pred evaluateWithObject:self];
    
    return isMatch? RegexStateRight:RegexStateChatError;
}

/*!
 *  验证密码格式输入是否正确
 */
- (RegexState)regexidenPsdNumber{
//    NSString * regex = @"\\w{6,26}";
    
//    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
//    if( [pred evaluateWithObject:self]){
//        return RegexStateRight;
//    }
    
    NSUInteger length = self.length ;
    if(length < 6){
        return RegexStateLengthToLessError;
    }else if(length >= 25){
        return RegexStateLengthToMoreError;
    }
    return RegexStateRight;

    return RegexStateChatError;

}

//姓名
- (RegexState) regexidenname
{
    NSString *nicknameRegex = @"^[\u4E00-\u9FA5]{2,5}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    if( [passWordPredicate evaluateWithObject:self]){
        int length = [NSString convertToInt:self];
        if(length < 2){
            return RegexStateLengthToLessError;
        }else if(length > 10){
            return RegexStateLengthToMoreError;
        }
        
        return RegexStateRight;
    }
    return RegexStateChatError;
}

//昵称
- (RegexState) regexidenNickname
{
    NSString *nicknameRegex = @"^[\u4E00-\u9FA5A-Za-z0-9_]+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    if( [passWordPredicate evaluateWithObject:self]){
        int length = [NSString convertToInt:self];
        if(length < 2){
            return RegexStateLengthToLessError;
        }else if(length > 10){
            return RegexStateLengthToMoreError;
        }
        
        return RegexStateRight;
    }
    return RegexStateChatError;
}

- (RegexState) regexidenSpeicalChat{
    NSString *nicknameRegex = @"^[A-Za-z0-9\u4E00-\u9FA5.,?!！。， ？ ?]+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    if( [passWordPredicate evaluateWithObject:self]){
     
        return RegexStateRight;
    }
    return RegexStateChatError;

}

- (RegexState) regexidenIdCard{
    NSString *nicknameRegex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    if( [passWordPredicate evaluateWithObject:self]){
        
        return RegexStateRight;
    }
    return RegexStateChatError;
    
}

/*!
 *  验证邮政编码
 */
- (RegexState)regexidenZipCode{

    NSString * regex = @"^[0-9]{6}$";
    
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    BOOL isMatch = [pred evaluateWithObject:self];
    
    return isMatch? RegexStateRight:RegexStateChatError;
}


+ (int)convertToInt:(NSString*)strtemp {
    
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
    }
    return (strlength+1);
    
}

////邮箱
//+ (BOOL) validateEmail:(NSString *)email
//{
//    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
//    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
//    return [emailTest evaluateWithObject:email];
//}
////手机号码验证
//+ (BOOL) validateMobile:(NSString *)mobile
//{
//    //手机号以13， 15，18开头，八个 \d 数字字符
//    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
//    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
//    return [phoneTest evaluateWithObject:mobile];
//}
////车牌号验证
//+ (BOOL) validateCarNo:(NSString *)carNo
//{
//    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
//    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
//    NSLog(@"carTest is %@",carTest);
//    return [carTest evaluateWithObject:carNo];
//}
////车型
//+ (BOOL) validateCarType:(NSString *)CarType
//{
//    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
//    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
//    return [carTest evaluateWithObject:CarType];
//}
////用户名
//+ (BOOL) validateUserName:(NSString *)name
//{
//    NSString *userNameRegex = @"^[A-Za-z0-9]{4,20}+$";
//    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
//    BOOL B = [userNamePredicate evaluateWithObject:name];
//    return B;
//}
////密码
//+ (BOOL) validatePassword:(NSString *)passWord
//{
//    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
//    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
//    return [passWordPredicate evaluateWithObject:passWord];
//}
////昵称
//+ (BOOL) validateNickname:(NSString *)nickname
//{
//    NSString *nicknameRegex = @"([\u4e00-\u9fa5]{2,5})(&middot;[\u4e00-\u9fa5]{2,5})*";
//    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
//    return [passWordPredicate evaluateWithObject:nickname];
//}
////身份证号
//+ (BOOL) validateIdentityCard: (NSString *)identityCard
//{
//    BOOL flag;
//    if (identityCard.length <= 0) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
//    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [identityCardPredicate evaluateWithObject:identityCard];
//}
////银行卡
//+ (BOOL) validateBankCardNumber: (NSString *)bankCardNumber
//{
//    BOOL flag;
//    if (bankCardNumber.length <= 0) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^(\\d{15,30})";
//    NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [bankCardPredicate evaluateWithObject:bankCardNumber];
//}
////银行卡后四位
//+ (BOOL) validateBankCardLastNumber: (NSString *)bankCardNumber
//{
//    BOOL flag;
//    if (bankCardNumber.length != 4) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^(\\d{4})";
//    NSPredicate *bankCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [bankCardPredicate evaluateWithObject:bankCardNumber];
//}
////CVN
//+ (BOOL) validateCVNCode: (NSString *)cvnCode
//{
//    BOOL flag;
//    if (cvnCode.length <= 0) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^(\\d{3})";
//    NSPredicate *cvnCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [cvnCodePredicate evaluateWithObject:cvnCode];
//}
////month
//+ (BOOL) validateMonth: (NSString *)month
//{
//    BOOL flag;
//    if (!month.length == 2) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"(^(0)([0-9])$)|(^(1)([0-2])$)";
//    NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [monthPredicate evaluateWithObject:month];
//}
////month
//+ (BOOL) validateYear: (NSString *)year
//{
//    BOOL flag;
//    if (!year.length == 2) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^([1-3])([0-9])$";
//    NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [yearPredicate evaluateWithObject:year];
//}
////verifyCode
//+ (BOOL) validateVerifyCode: (NSString *)verifyCode
//{
//    BOOL flag;
//    if (!verifyCode.length == 6) {
//        flag = NO;
//        return flag;
//        }
//    NSString *regex2 = @"^(\\d{6})";
//    NSPredicate *verifyCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
//    return [verifyCodePredicate evaluateWithObject:verifyCode];
//}

@end
